package java44th.group.service.admin.order;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.admin.bo.order.OrderRefundBO;
import java44th.group.bean.admin.bo.order.OrderShipBO;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.*;
import java44th.group.bean.admin.vo.order.CourierVO;
import java44th.group.bean.admin.vo.order.OrderDetailVO;
import java44th.group.bean.admin.vo.order.OrderListVO;
import java44th.group.bean.admin.vo.order.UserInfoInnerOrderDetailVO;
import java44th.group.mapper.MarketCommentMapper;
import java44th.group.mapper.MarketOrderGoodsMapper;
import java44th.group.mapper.MarketOrderMapper;
import java44th.group.mapper.MarketUserMapper;
import java44th.group.utils.ReflectUtils;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 小焦
 * @since 2022/10/11 21:18
 */

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;
    @Autowired
    MarketCommentMapper commentMapper;


    /**
     * 查询所有订单记录
     * @param pageInfo 分页:page/limit/  排序:sort/WxOrderController,借助example
     * @param orderId
     * @param start
     * @param end
     * @param userId
     * @param orderSn
     * @return java44th.group.bean.common.CommonData<java44th.group.bean.po.MarketOrder>
     * @author 小焦
     * @since 2022/10/12 下午 03:49
     */
    @Override
    public CommonData<OrderListVO> list(BasePageInfo pageInfo, Integer[] orderStatusArray, Integer orderId, Date start, Date end, Integer userId, String orderSn) {
        // 分页插件 PageHelper，辅助我们做分页以及分页信息的获得,在我们需要分页的方法下写以下配置
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());

        MarketOrderExample orderExample = new MarketOrderExample();
        orderExample.setOrderByClause(pageInfo.getSort() + " " + pageInfo.getOrder());
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (orderId != null) {
            criteria.andIdEqualTo(orderId);
        }
        if(!StringUtil.isEmpty(orderSn)){
            criteria.andOrderSnLike("%"+orderSn+"%");
        }
        if (start != null || end != null) {
            criteria.andAddTimeBetween(start, end);
        }
        criteria.andDeletedEqualTo(false);
        int orderStatus=0;
        int i;
        if(orderStatusArray!=null&& orderStatusArray.length>0){
            for ( i = 0; i < orderStatusArray.length; i++) {
                if(orderStatusArray[i]!=null){
                    orderStatus=orderStatusArray[i];
                }
            }
            criteria.andOrderStatusEqualTo((short) orderStatus);
        }
        List<MarketOrder> list = orderMapper.selectByExample(orderExample);
        List<OrderListVO> listVOS = ReflectUtils.copyProperties(list, OrderListVO.class);
        PageInfo<OrderListVO> orderPageInfo = new PageInfo<>(listVOS);

        return CommonData.data(orderPageInfo);
    }

    @Override
    public List<CourierVO> channel() {
        List<CourierVO> list = new ArrayList<>();
        list.add(new CourierVO("ZTO", "中通快递"));
        list.add(new CourierVO("YTO", "圆通速递"));
        list.add(new CourierVO("YD", "韵达速递"));
        list.add(new CourierVO("YZPY", "邮政快递包裹"));
        list.add(new CourierVO("EMS", "EMS"));
        list.add(new CourierVO("DBL", "德邦快递"));
        list.add(new CourierVO("FAST", "快捷快递"));
        list.add(new CourierVO("ZJS", "宅急送"));
        list.add(new CourierVO("TNT", "TNT快递"));
        list.add(new CourierVO("UPS", "UPS"));
        list.add(new CourierVO("DHL", "DHL"));
        list.add(new CourierVO("FEDEX", "FEDEX联邦(国内件)"));
        list.add(new CourierVO("FEDEX_GJ", "FEDEX联邦(国际件)"));


        return list;
    }

    /**
     *  获取订单详情接口实现
     * @param id 订单id
     * @return java44th.group.bean.admin.vo.WxOrderController.OrderDetailVO
     * @author 小焦
     * @since 2022/10/13 上午 11:41
     */
    @Override
    public OrderDetailVO detail(Integer id) {
        MarketOrder order = orderMapper.selectByPrimaryKey(id);
        MarketUser marketUser = userMapper.selectByPrimaryKey(order.getUserId());
        UserInfoInnerOrderDetailVO user = new UserInfoInnerOrderDetailVO();
        user.setNickname(marketUser.getNickname());
        user.setAvatar(marketUser.getAvatar());
        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);

        List<MarketOrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);
        OrderDetailVO orderDetailVO = new OrderDetailVO();
        orderDetailVO.setOrder(order);
        orderDetailVO.setOrderGoods(orderGoodsList);
        orderDetailVO.setUser(user);
        return orderDetailVO;
    }

    /**
     *  退款接口
     * @param orderRefundBO 订单id和退款金额
     * @return int
     * @author 小焦
     * @since 2022/10/12 下午 09:19
     */
    @Override
    public int refund(OrderRefundBO orderRefundBO) {
        MarketOrder  order = new MarketOrder();
        order.setRefundAmount(orderRefundBO.getRefundMoney());
        order.setOrderStatus((short)203);
        order.setId(orderRefundBO.getOrderId());
        order.setRefundTime(new Timestamp(System.currentTimeMillis()));
        order.setRefundType("微信退款接口");
        order.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        int count = orderMapper.updateByPrimaryKeySelective(order);
        if(count==1){
            return count;
        }
        return 0;
    }

    /**
     *  发货接口的实现{"orderId":539,"shipChannel":"ZTO","shipSn":"123"}
     * @param orderShipBO
     * @return int
     * @author 小焦
     * @since 2022/10/12 下午 10:03
     */
    @Override
    public int ship(OrderShipBO orderShipBO) {
        MarketOrder order = new MarketOrder();
        order.setId(orderShipBO.getOrderId());
        order.setOrderStatus((short)301);
        order.setShipChannel(orderShipBO.getShipChannel());
        order.setShipSn(orderShipBO.getShipSn());
        order.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        int count = orderMapper.updateByPrimaryKeySelective(order);
        if(count==1){
            return count;
        }
        return 0;
    }

    @Override
    public int reply(java44th.group.bean.bo.order.OrderReplyBO orderReplyBO) {

        MarketComment comment = commentMapper.selectByPrimaryKey(orderReplyBO.getCommentId());
        if(!StringUtil.isEmpty(comment.getContent())){
            return 622;
        }
        MarketComment marketComment = new MarketComment();
        marketComment.setId(orderReplyBO.getCommentId());
        marketComment.setContent(orderReplyBO.getContent());
        marketComment.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        int count = commentMapper.updateByPrimaryKeySelective(marketComment);
        if(count==1){
            return count;
        }
        return 0;
    }

    @Override
    public int delete(Integer orderId) {
        MarketOrder order = orderMapper.selectByPrimaryKey(orderId);
        int count;
        if(order.getOrderStatus()==102 || order.getOrderStatus()==103 ||order.getOrderStatus()==203||order.getOrderStatus()==401||order.getOrderStatus()==402){
            MarketOrder marketOrder = new MarketOrder();
            marketOrder.setDeleted(true);
            marketOrder.setEndTime(new Timestamp(System.currentTimeMillis()));
             count = orderMapper.updateByPrimaryKeySelective(marketOrder);
             return count;
        }

        return 0;
    }

}
